package com.xwwx.common;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/*******************************************************************************
 * Created by Eclipse. User: 李伟 Date: 2007-03-7 功能: 通用时间相关类
 ******************************************************************************/
public class CommTime {
	//得到新闻文件名
	public static String getNewTime(){
        SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyyMMddHHmmss");
        GregorianCalendar gc=new GregorianCalendar();
        return dateFormatter.format(gc.getTime());
	}
	public static String getCurrTime() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat(
				"yyyy-MM-dd-HH:mm:ss");
		GregorianCalendar gc = new GregorianCalendar();
		return dateFormatter.format(gc.getTime());
	}
	public static String getCurrTime2() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		GregorianCalendar gc = new GregorianCalendar();
		return dateFormatter.format(gc.getTime());
	}
	public static String getCurrTime1() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss");
		GregorianCalendar gc = new GregorianCalendar();
		return dateFormatter.format(gc.getTime());
	}

	public static String getCurrDate() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar gc = new GregorianCalendar();
		return dateFormatter.format(gc.getTime());
	}

	public static String getCurrDate2() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy年MM月dd日");
		GregorianCalendar gc = new GregorianCalendar();
		return dateFormatter.format(gc.getTime());
	}

	public static String getCurrDate1() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
		GregorianCalendar gc = new GregorianCalendar();
		return dateFormatter.format(gc.getTime());
	}

	public static String getNewDate() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMM");
		GregorianCalendar gc = new GregorianCalendar();
		return dateFormatter.format(gc.getTime());
	}

	public static int getYear() {
		Calendar CD = Calendar.getInstance();
		int YY = CD.get(Calendar.YEAR);

		return YY;
	}

	public static int getmonth() {
		Calendar CD = Calendar.getInstance();
		int MM = CD.get(Calendar.MONTH) + 1;
		return MM;
	}

	@SuppressWarnings("deprecation")
	public static String getYearMonthDay(String str) {
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat dateFormatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String temp = "";
		Date date = null;
		try {
			date = dateFormatter.parse(str);
		} catch (Exception e) {
			//System.out.print(e.getMessage());
		}
		temp = sb.append((date.getYear() + 1900))
				 .append("年")
				 .append((date.getMonth() + 1))
				 .append("月")
				 .append(date.getDate())
				 .append("日").toString();

		sb = null;
		return temp;
	}
	@SuppressWarnings("deprecation")
	public static String getYearMonthDay1(String str) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat(
				"yyyy-MM-dd_HH:mm:ss");
		String temp = "";
		Date date = null;
		try {
			date = dateFormatter.parse(str);
		} catch (Exception e) {
			System.out.print(e.getMessage());
		}
		StringBuffer sb = new StringBuffer();
		
		temp = sb.append((date.getYear() + 1900))
		 .append("年")
		 .append((date.getMonth() + 1))
		 .append("月")
		 .append(date.getDate())
		 .append("日").toString();
		
		sb = null;
		return temp;
	}
	// 根据字符串得到date类型
	public static Date getDate(String str) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = dateFormatter.parse(str);
		} catch (Exception e) {
			System.out.print(e.getMessage());
		}
		return date;
	}

	// 根据当前时间得到一个时间段后的日期格式
	public static String getFutureDate(long millisecond) {

		Date date = new Date();
		Date date1 = new Date(date.getTime() + millisecond);

		SimpleDateFormat dateFormatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String time = dateFormatter.format(date1);
		return time;

	}
	// 根据当前时间得到一个时间段前的日期格式
	public static String getBeforeDate(long millisecond) {

		Date date = new Date();
		Date date1 = new Date(date.getTime() - millisecond);

		SimpleDateFormat dateFormatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String time = dateFormatter.format(date1);
		return time;

	}
	//将字符串转化为date格式
	public static Date getDateBystr(String str){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		try {
			date = format.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	//将字符串转化为date格式
	public static String getDateBystr1(String str){  
		String sdate="";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");   
		try{
			Date d = df.parse(str);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");   
		    sdate = sdf.format(d);
		}catch(Exception e){
			e.printStackTrace();
		}
		return sdate;
	}	
	//将字符串转化为date格式
	public static String getDateBystr2(String str){  
		String sdate="";
		String stime="";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");   
		try{
			Date d = df.parse(str);
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf2 = new SimpleDateFormat("HH:ss:mm"); 
		    sdate = sdf1.format(d);
		    stime = sdf2.format(d);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return new StringBuffer().append(sdate).append("_").append(stime).toString();
	}	
	public static String[] getWeekRq(String str){
		String[] rqweek = new String[7];
		try{
			Calendar c = Calendar.getInstance();
			Calendar c1 = Calendar.getInstance();
			Date d = getDate(str);
			c.setTime(d);
			c1.setTime(d);
			//得到输入的日期是一周的第几天
            int dow = c.get(Calendar.DAY_OF_WEEK)-1;
            //System.out.println("一周第"+dow+"天");
            
            SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
            //System.out.println("日期:"+dateFormatter.format(c.getTime()));
            
            switch (dow){
            	case 1 : 
            		rqweek[0] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, 1);
            		rqweek[1] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, 1);  
            		rqweek[2] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, 1); 
            		rqweek[3] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, 1);  
            		rqweek[4] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, 1);  
            		rqweek[5] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, 1);  
            		rqweek[6] = dateFormatter.format(c.getTime());
            		break;
            	case 2 : 
            		rqweek[1] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[0] = dateFormatter.format(c.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[2] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[3] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[4] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[5] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[6] = dateFormatter.format(c1.getTime());
            		break;
            	case 3 : 
            		rqweek[2] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[1] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[0] = dateFormatter.format(c.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[3] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[4] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[5] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[6] = dateFormatter.format(c1.getTime());          		
            		break;
            	case 4 : 
            		rqweek[3] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[2] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[1] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[0] = dateFormatter.format(c.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[4] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[5] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[6] = dateFormatter.format(c1.getTime());
            		break;
            	case 5 : 
            		rqweek[4] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[3] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[2] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[1] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[0] = dateFormatter.format(c.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[5] = dateFormatter.format(c1.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[6] = dateFormatter.format(c1.getTime());
            		break;
            	case 6 : 
            		rqweek[5] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[4] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[3] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[2] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[1] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[0] = dateFormatter.format(c.getTime());
            		c1.add(Calendar.DATE, 1);
            		rqweek[6] = dateFormatter.format(c1.getTime()); 
            		break;
            	case 0 : 
            		rqweek[6] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[5] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[4] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[3] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[2] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[1] = dateFormatter.format(c.getTime());
            		c.add(Calendar.DATE, -1);
            		rqweek[0] = dateFormatter.format(c.getTime()); 
            		break;
            }

//			for (int i = 0; i < 7; i++) {
//				System.out.print(rqweek[i]);
//			}

		}catch(Exception e){
			e.printStackTrace();
		}
		
		return rqweek;
	}
	public static void main(String args[]) {
		// System.out.println(CommTime.getYear());
		// System.out.println(CommTime.getmonth());
		// System.out.println(CommTime.getNewDate());
		//
		/*
		 System.out.println(CommTime.getYearMonthDay("2008-03-07 14:19:42"));
		//
		// System.out.println(CommTime.getDate("2008-08-09"));
		System.out.println(CommTime.getFutureDate(24 * 60 * 60 * 1000));

		String str = "2008-4-1 09:25:10.0";   
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");   
		try{
			Date d = df.parse(str);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");   
		    String sdate = sdf.format(d);
		    System.out.println(sdate);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		//
		Calendar cd = Calendar.getInstance();
		
*/
		System.out.println(CommTime.getBeforeDate(Constant.logintime * 1000 * 60));
	}
}